<template>
  <div class="settings-container">
    <div class="settings-header">
      <h2>设置</h2>
      <button class="close-btn" @click="$emit('close')">
        <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
          <path d="M12 4L4 12M4 4L12 12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
        </svg>
      </button>
    </div>
    
    <div class="settings-content">
      <div class="setting-group">
        <h3>编辑器设置</h3>
        <div class="setting-item">
          <label class="setting-label">
            <input 
              type="checkbox" 
              v-model="settings.autoSave"
              class="checkbox-input"
            />
            <span class="checkmark"></span>
            自动保存
          </label>
          <p class="setting-desc">每2秒自动保存当前编辑的内容</p>
        </div>
      </div>

      <div class="setting-group">
        <h3>文件设置</h3>
        <div class="setting-item">
          <label class="setting-label">默认文件路径</label>
          <div class="path-input-group">
            <input 
              type="text" 
              v-model="settings.defaultDirectory"
              placeholder="选择默认保存路径"
              class="path-input"
              readonly
            />
            <button @click="selectFolder" class="browse-btn">浏览</button>
          </div>
          <p class="setting-desc">新文件将默认保存在此路径</p>
        </div>
      </div>

      <div class="settings-actions">
        <button @click="resetSettings" class="btn btn-secondary">重置</button>
        <button @click="saveAndClose" class="btn btn-primary">保存</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useSettings } from '../composables/useSettings.js'
import { useModal } from '../composables/useModal.js'

const emit = defineEmits(['close'])
const { settings, resetSettings, saveSettings, selectFolder } = useSettings()
const { showConfirm } = useModal()

const handleReset = async () => {
  const confirmed = await showConfirm(
    '重置设置',
    '确定要重置所有设置为默认值吗？此操作不可撤销。',
    'warning'
  )
  
  if (confirmed) {
    await resetSettings()
  }
}

const saveAndClose = async () => {
  await saveSettings()
  emit('close')
}

onMounted(async () => {
  await useSettings().loadSettings()
})
</script>

<style scoped>
.settings-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  max-width: 90vw;
  max-height: 80vh;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  display: flex;
  flex-direction: column;
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e8e8e8;
}

.settings-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #262626;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: #8c8c8c;
  transition: color 0.3s;
}

.close-btn:hover {
  color: #262626;
}

.settings-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
}

.setting-group {
  margin-bottom: 32px;
}

.setting-group:last-child {
  margin-bottom: 0;
}

.setting-group h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}

.setting-item {
  margin-bottom: 20px;
}

.setting-label {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: 500;
  color: #262626;
  cursor: pointer;
}

.checkbox-input {
  display: none;
}

.checkmark {
  width: 16px;
  height: 16px;
  border: 2px solid #d9d9d9;
  border-radius: 3px;
  margin-right: 8px;
  position: relative;
  transition: all 0.3s;
}

.checkbox-input:checked + .checkmark {
  background-color: #1890ff;
  border-color: #1890ff;
}

.checkbox-input:checked + .checkmark::after {
  content: '';
  position: absolute;
  left: 3px;
  top: 1px;
  width: 6px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.setting-desc {
  margin: 4px 0 0 24px;
  font-size: 12px;
  color: #8c8c8c;
}

.path-input-group {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.path-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  color: #262626;
  background-color: #fafafa;
}

.path-input:focus {
  outline: none;
  border-color: #1890ff;
}

.browse-btn {
  padding: 8px 16px;
  background: #f5f5f5;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  color: #262626;
  cursor: pointer;
  transition: all 0.3s;
}

.browse-btn:hover {
  background: #e6e6e6;
  border-color: #b3b3b3;
}

.settings-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid transparent;
}

.btn-primary {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

.btn-primary:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.btn-secondary {
  background: white;
  color: #262626;
  border-color: #d9d9d9;
}

.btn-secondary:hover {
  background: #f5f5f5;
  border-color: #b3b3b3;
}

/* 滚动条样式 */
.settings-content::-webkit-scrollbar {
  width: 6px;
}

.settings-content::-webkit-scrollbar-track {
  background: #f5f5f5;
  border-radius: 3px;
}

.settings-content::-webkit-scrollbar-thumb {
  background: #d9d9d9;
  border-radius: 3px;
}

.settings-content::-webkit-scrollbar-thumb:hover {
  background: #b3b3b3;
}
</style>